#include <stdio.h>
#include "stack.h"
#include <stdlib.h>

// 优先级二维数组表(1表示大于，-1表示小于,0表示等于,-2表示错误)
int priority[7][7] =
    { //  +  -   *    /  (  ) #
        {1, 1, -1, -1, -1, 1, 1},
        {1, 1, -1, -1, -1, 1, 1},
        {1, 1, 1, 1, -1, 1, 1},
        {1, 1, 1, 1, -1, 1, 1},
        {-1, -1, -1, -1, -1, 0, -2},
        {1, 1, 1, 1, -2, 1, 1},
        {-1, -1, -1, -1, -1, -2, 0}};

int priority_comparison(SElemType *e, char ch)
{
    //*e为字符的ascil码,ch也是ascil码
    // 分别获取优先级
    int i = 0, j = 0;
    char op = *e;
    switch (op)
    {
    case '+':
    {
        i = 0;
        break;
    }
    case '-':
    {
        i = 1;
        break;
    }
    case '*':
    {
        i = 2;
        break;
    }
    case '/':
    {
        i = 3;
        break;
    }
    case '(':
    {
        i = 4;
        break;
    }
    case ')':
    {
        i = 5;
        break;
    }
    case '#':
    {
        i = 6;
        break;
    }
    }
    switch (ch)
    {
    case '+':
    {
        j = 0;
        break;
    }
    case '-':
    {
        j = 1;
        break;
    }
    case '*':
    {
        j = 2;
        break;
    }
    case '/':
    {
        j = 3;
        break;
    }
    case '(':
    {
        j = 4;
        break;
    }
    case ')':
    {
        j = 5;
        break;
    }
    case '#':
    {
        j = 6;
        break;
    }
    }
    return priority[i][j]; // 返回1,0,-1(表示大于,等于,小于)
}

SElemType do_operator(SElemType *data1, SElemType *operator, SElemType * data2)
{
    char op = (*operator);
    if (op == '+')
    {
        return (*data1) + (*data2);
    }
    else if (op == '-')
    {
        return (*data1) - (*data2);
    }
    else if (op == '*')
    {
        return (*data1) * (*data2);
    }
    else if (op == '/')
    {
        return (*data1) / (*data2);
    }
}

int main()
{
    SqStack *OFTR = (SqStack *)malloc(sizeof(SqStack)); // 用来寄存运算符
    SqStack *OFND = (SqStack *)malloc(sizeof(SqStack)); // 用来计算操作数或结果

    // 初始化
    InitStack(OFND);
    InitStack(OFTR);

    // 输入:{输入格式3*(7-2)# [#是结束标识符]}
    char ch = getchar();
    push(OFTR, '#'); // 开始标志符先压入栈
    SElemType *oftr_top = (SElemType *)malloc(sizeof(SElemType));
    printf("比较优先级为:");
    Get_top(OFTR, oftr_top);                // 判断出口用
    while ((*oftr_top) != '#' || ch != '#') // 如果OFTR不空(操作符还有就继续)或者未输入结束标识符就继续
    {
        // priority_comparison(e,ch)
        if (ch >= '0' && ch <= '9') // ch如果是操作数
        {
            push(OFND, ch - 48);
            ch = getchar();
        }
        else
        {
            SElemType *e = (SElemType *)malloc(sizeof(SElemType));
            Get_top(OFTR, e); // e是栈顶操作符
            printf("%d ", priority_comparison(e, ch));
            if (priority_comparison(e, ch) == 1) // e的优先级大于ch
            {
                // 进行运算
                SElemType *data1 = (SElemType *)malloc(sizeof(SElemType));
                SElemType *data2 = (SElemType *)malloc(sizeof(SElemType));
                SElemType *operator=(SElemType *) malloc(sizeof(SElemType));
                pop(OFTR, operator);
                pop(OFND, data1);
                pop(OFND, data2);
                SElemType data = do_operator(data2, operator, data1); // 进行运算
                push(OFND, data);
                // ch = getchar();
            }
            else if (priority_comparison(e, ch) == 0) // 两者优先级相同
            {
                SElemType *e2 = (SElemType *)malloc(sizeof(SElemType));
                pop(OFTR, e2);
                free(e2);
                ch = getchar();
            }
            else // e的优先级小于ch
            {
                push(OFTR, ch);
                ch = getchar();
            }
        }
        Get_top(OFTR, oftr_top);
    }
    // clear_SqStack(OFND);
    printf("\n等式结果为:%d", *((*OFND).top));
}